﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class D_HomeField
    {
        public bool CreateOneHomeField(HomeField_Info objHomeField_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("HomeField_Insert", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"HomeFieldID", objHomeField_Info.HomeFieldID));
            command.Parameters.Add(new SqlParameter(@"HomeFiledName", objHomeField_Info.HomeFiledName));
            command.Parameters.Add(new SqlParameter(@"PasteurisingDate", objHomeField_Info.PasteurisingDate));
            command.Parameters.Add(new SqlParameter(@"BuildingID", objHomeField_Info.BuildingID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //**********************************************************************************       
        public bool EditOneHomeField(HomeField_Info objHomeField_Info)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("HomeField_Update", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@HomeFieldID", objHomeField_Info.HomeFieldID));
            command.Parameters.Add(new SqlParameter("@HomeFiledName", objHomeField_Info.HomeFiledName));
            command.Parameters.Add(new SqlParameter("@PasteurisingDate", objHomeField_Info.PasteurisingDate));
            command.Parameters.Add(new SqlParameter("@BuildingID", objHomeField_Info.BuildingID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public bool RemoveOneHomeField(int HomeFieldID)
        {
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("HomeField_Delete", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"HomeFieldID", HomeFieldID));
            bool isOk = false;
            try
            {
                objConnection.Open();
                int k = command.ExecuteNonQuery();
                if (k > 0)
                    isOk = true;
            }
            catch (System.Exception ex)
            {
                isOk = false;
            }
            finally
            {
                objConnection.Close();
            }
            return isOk;
        }
        //******************************************************************************       
        public DataTable GetAllHomeField()
        {
            DataSet ds = new DataSet();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("HomeField_SelectAll", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            try
            {
                objConnection.Open();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(ds);
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return ds.Tables[0];
        }
        //******************************************************************************       
        public HomeField_Info GetOneHomeField(int HomeFieldID)
        {
            HomeField_Info objHomeField_Info = new HomeField_Info();
            CommonDAL objCommonDAL = new CommonDAL();
            SqlConnection objConnection = objCommonDAL.GetConnection();
            SqlCommand command = new SqlCommand("HomeField_SelectByID", objConnection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter(@"HomeFieldID", HomeFieldID));
            try
            {
                objConnection.Open();
                SqlDataReader ordHomeField = command.ExecuteReader(); //OjectRecordDatareader
                ordHomeField.Read();
                if (ordHomeField.HasRows)
                {
                    if (ordHomeField["HomeFieldID"] != DBNull.Value)
                    {
                        objHomeField_Info.HomeFieldID = Convert.ToInt16(ordHomeField["HomeFieldID"]);
                    }
                    if (ordHomeField["HomeFiledName"] != DBNull.Value)
                    {
                        objHomeField_Info.HomeFiledName = Convert.ToString(ordHomeField["HomeFiledName"]);
                    }
                    if (ordHomeField["PasteurisingDate"] != DBNull.Value)
                    {
                        objHomeField_Info.PasteurisingDate = Convert.ToDateTime(ordHomeField["PasteurisingDate"]);
                    }
                    if (ordHomeField["BuildingID"] != DBNull.Value)
                    {
                        objHomeField_Info.BuildingID = Convert.ToInt16(ordHomeField["BuildingID"]);
                    }
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            finally
            {
                objConnection.Close();
            }
            return objHomeField_Info;
        }
    }
}
